<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\library\repositories\Login;
use App\library\services\Jwt;
use App\library\services\lib\Sms;
use App\library\services\WeChatService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\library\common;

class WxLogin extends Controller
{
    /**
     * Author:Yingbo Sun
     * 获取token
     * @param Request $request
     * @return array|string
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function WeChatLogin(Request $request)
    {
        $code = $request->post('code','0');
        if (empty($code)){
            return $this->fail('参数错误');
        }
        $token = WeChatService::WeChatLogin($code,'user');
        return $this->success('获取成功',$token);
    }

    /**
     * Author:Yingbo Sun
     * 授权获取用户信息
     * @param Request $request
     * @return array
     */
    public function userInfo(Request $request)
    {
        $request->validate([
            'nick_name' => 'required',
            'sex' => 'required',
            'avatar' => 'required',
        ],
            [
            'nick_name.required' => '昵称必须',
            'sex.required' => '性别必须',
            'avatar.required' => '头像必须',
        ]);
        $params = $request->all();
        $uid = $request->uid;
        $data = Login::login($params,$uid,'user');
        return $data;
    }

    public function agentInfo(Request $request)
    {
        $params = $request->all();
        $uid = $request->uid;
        $data = Login::login($params,$uid,'agent');
        return $data;
    }

    /**
     * Author:Yingbo Sun
     * 刷新token
     * @param Request $request
     * @return mixed
     */
    public function refreshToken(Request $request)
    {
        $uid = $request->post('uid');
        if (empty($uid)){
            return $this->fail('参数错误');
        }
        $data = Jwt::refreshToken($uid);
        return $this->res($data);
    }

    /**
     * Author:Yingbo Sun
     * 经纪人获取token
     * @param Request $request
     * @return array|string
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function agentLogin(Request $request)
    {
        $code = $request->post('code','0');

        if (empty($code)){
            return $this->fail('参数错误');
        }
        $token = WeChatService::WeChatLogin($code,'agent');
        return $this->success('获取成功',$token);
    }

    /**
     * Author:Yingbo Sun
     * 发送验证码
     * @param Request $request
     * @return mixed
     */
    public function sendSms(Request $request)
    {
        $phone = $request->post('phone','0');
        if (empty($phone)){
            return self::fail('参数错误');
        }
        //正则验证手机
        if (!verifyPhone($phone)){
            return self::fail('请输入正确的手机号！');
        }
        $code = produceCode();
        Redis::select(2);
        //60秒内只能获取一次验证码
        if (Redis::get("{$phone}:code:disableTime")) {
            return self::fail('60秒之内只能发一次验证码');
        }
        if (Redis::get("{$phone}:code:count") > 15){
            return self::fail('一天内只能发送15次验证码');
        }
//        $data = Sms::sendSms($phone,$code);
        $data['code'] = 200;
        echo $code;
        if ($data['code'] == 200){
            //EX 过期时间方式 秒
            Redis::set("{$phone}:code", $code,'EX',60);
            Redis::set("{$phone}:code:disableTime",60,'EX',60);
            if (!Redis::exists("{$phone}:code:count")){
                $time = strtotime(date('Y-m-d 23:59:59',time())) - time();
                Redis::set("{$phone}:code:count",1,'EX',$time);
            }else{
                Redis::incr("{$phone}:code:count");
            }
            return self::success('发送成功');
        }
        return self::fail('发送失败');
    }

    /**
     * Author:Yingbo Sun
     * 经纪人短信验证登录
     * @param Request $request
     * @return mixed
     */
    public function phoneLogin(Request $request)
    {
        $uid = $request->uid;
        $phone = $request->post('phone','0');
        $code = $request->post('code','0');
        if (empty($phone) || empty($code) || empty($uid)){
            return self::fail('参数错误');
        }
        //正则验证手机
        if (!verifyPhone($phone)){
            return self::fail('手机号不正确！');
        }
        Redis::select(2);
        $verifyCode = Redis::get("{$phone}:code");
        if ($verifyCode != $code){
            return self::fail('验证码错误');
        }
        $data = Login::agentLogin($phone,$uid);
        return $this->res($data);
    }

}
